<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("Writer.php");
//Includes
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Veh_Reports_Company extends Page
{
       public $JTGridReports = null;
       public $JTSiteTheme1 = null;
       public $hiddentype = null;
       public $hiddenReportDate = null;
       function Veh_Reports_CompanyShow($sender, $params)
       {
       $company = $_SESSION['company_id'];
       $report = $_GET['report_date'];
       $type = $_GET['type'];
       $dec = $_GET['dec'];

       $this->hiddenReportDate->setValue($report);
       $this->hiddentype->setValue($type);
       $report_date = $this->hiddenReportDate->getValue();
       $report_type = $this->hiddentype->getValue();

       $arr = array();
       $company_total = 0;

       if($dec!='1')
       $query_veh = "select vehicle_id, veh_num from vehicle_tbl where out_of_service='0' AND company_id='".$company."'";
       if($dec=='1')
       $query_veh = "select vehicle_id, veh_num from vehicle_tbl where out_of_service='1' AND company_id='".$company."'";

       $result_veh = mysql_query($query_veh)or die(mysql_error());
       while($row_veh = mysql_fetch_array($result_veh))
       {
        $veh_id = $row_veh['vehicle_id'];
        $plate = $row_veh['veh_num'];
        $veh_total = 0;

        $veh_fuel_total = 0;
        $query_veh_fuel = "SELECT tot_cost FROM veh_fuel_tbl WHERE vehicle_id='".$veh_id."' AND date LIKE '".$report_date."'";
        $result_veh_fuel = mysql_query($query_veh_fuel) or die("SELECT FUEL ".mysql_error());
        while($row_veh_fuel = mysql_fetch_array($result_veh_fuel))
        {
         $veh_fuel_total+=$row_veh_fuel['tot_cost'];
        }

        $veh_labor_total = 0;
        $query_veh_labor = "SELECT total FROM veh_laborline_tbl WHERE vehicle_id='".$veh_id."' AND labor_date LIKE '".$report_date."'";
        $result_veh_labor = mysql_query($query_veh_labor) or die("SELECT LABORLINE ".mysql_error());
        while($row_veh_labor = mysql_fetch_array($result_veh_labor))
        {
         $veh_labor_total+=$row_veh_labor['total'];
        }

        $veh_parts_total = 0;
        $query_veh_parts = "SELECT amount FROM veh_parts_tbl WHERE vehicle_id='".$veh_id."' AND parts_date LIKE '".$report_date."'";
        $result_veh_parts = mysql_query($query_veh_parts) or die("SELECT PARTS ".mysql_error());
        while($row_veh_parts = mysql_fetch_array($result_veh_parts))
        {
         $veh_parts_total+=$row_veh_parts['amount'];
        }

        $veh_expense_total = 0;
        $query_veh_expense = "SELECT amount FROM veh_expense_tbl WHERE vehicle_id='".$veh_id."' AND date LIKE '".$report_date."'";
        $result_veh_expense = mysql_query($query_veh_expense) or die("SELECT PARTS ".mysql_error());
        while($row_veh_expense = mysql_fetch_array($result_veh_expense))
        {
         $veh_expense_total+=$row_veh_expense['amount'];
        }

        $veh_emission_total = 0;
        $query_veh_emission = "SELECT test_fee FROM veh_emission_tbl WHERE vehicle_id='".$veh_id."' AND inspection_date LIKE '".$report_date."'";
        $result_veh_emission = mysql_query($query_veh_emission) or die("SELECT PARTS ".mysql_error());
        while($row_veh_emission = mysql_fetch_array($result_veh_emission))
        {
         $veh_emission_total+=$row_veh_emission['test_fee'];
        }

        $veh_total = $veh_fuel_total+
        $veh_labor_total+
        $veh_parts_total+
        $veh_expense_total+
        $veh_emission_total;

        $company_total+= $veh_total;
        $arr[] = array('Vehicle'=>$plate,
        'Fuel'=>$veh_fuel_total,
        'Laborline'=>$veh_labor_total,
        'Parts'=>$veh_parts_total,
        'Expense'=>$veh_expense_total,
        'Emission'=>$veh_emission_total,
        'Total'=>$veh_total);
       }
       $arr[] = array('Vehicle'=>'',
        'Fuel'=>'',
        'Laborline'=>'',
        'Parts'=>'',
        'Expense'=>'',
        'Emission'=>'Total',
        'Total'=>$company_total);
       $this->JTGridReports->setCellData($arr);
       $this->JTGridReports->exportGridToXLSDownload('Vehicle_Report.xls');
       }

}

global $application;

global $Veh_Reports_Company;

//Creates the form
$Veh_Reports_Company=new Veh_Reports_Company($application);

//Read from resource file
$Veh_Reports_Company->loadResource(__FILE__);


//Shows the form
$Veh_Reports_Company->show();

?>